using Godot;
using System;

namespace Box;

public partial class TreeTabContainer : TabContainer
{
	[Signal]
	public delegate void onSelectTabEventHandler(ProcessTreeGraphEditor tab);

	public PropertyPanel PropertyPanel;

	Control AddIconControl;
	PackedScene ProcessTreeGraphEditorPackedScene;
	public int AddTreeButtonIdx = 0;
	Texture2D AddIcon;
	Texture2D RemoveIcon;

	public override void _Ready()
	{
		ProcessTreeGraphEditorPackedScene = GD.Load<PackedScene>("res://source/process_tree_editor/process_tree_graph_editor/ProcessTreeGraphEditor.tscn");
		RemoveIcon = GD.Load<Texture2D>("res://resource/image/icon/remove_icon.png");
		AddIcon = GD.Load<Texture2D>("res://resource/image/icon/add_icon.png");

		AddIconControl = GetTabControl(AddTreeButtonIdx);

		SetTabTitle(AddTreeButtonIdx,"");
		SetTabButtonIcon(AddTreeButtonIdx,AddIcon);
		SetTabDisabled(AddTreeButtonIdx,true);

		TabButtonPressed += _onAddTree;

		TabSelected += _onTabSelected;
		TabChanged += _onTabSelected;
	}

	public void _onTabSelected(long idx) {
		Control control = GetTabControl((int)idx);
		if(control is ProcessTreeGraphEditor) {
			EmitSignal(nameof(onSelectTab),(ProcessTreeGraphEditor)control);
		}
	}

	public void Clear() {
		AddTreeButtonIdx = 0;

		foreach(Node node in GetChildren()) {
			RemoveChild(node);
		}

		AddChild(AddIconControl);
		SetTabTitle(AddTreeButtonIdx,"");
		SetTabButtonIcon(AddTreeButtonIdx,AddIcon);
		SetTabDisabled(AddTreeButtonIdx,true);
	}
	public void AddTab(ProcessTreeGraphEditor tree) {
		RemoveChild(AddIconControl);
		AddChild(tree);
		AddChild(AddIconControl);

		SetTabButtonIcon(AddTreeButtonIdx,RemoveIcon);
		SetTabDisabled(AddTreeButtonIdx,false);

		AddTreeButtonIdx++;

		SetTabTitle(AddTreeButtonIdx,"");
		SetTabButtonIcon(AddTreeButtonIdx,AddIcon);
		SetTabDisabled(AddTreeButtonIdx,true);
	}

	public void AddTab(string? name = null) {
		var new_tree = ProcessTreeGraphEditorPackedScene.Instantiate<ProcessTreeGraphEditor>();
		new_tree.PropertyPanel = PropertyPanel;

		if(name == null) {
			new_tree.Name = $"Tree{new_tree.GetInstanceId()}";
		} else {
			new_tree.Name = name;
		}

		RemoveChild(AddIconControl);
		AddChild(new_tree);
		AddChild(AddIconControl);

		SetTabButtonIcon(AddTreeButtonIdx,RemoveIcon);
		SetTabDisabled(AddTreeButtonIdx,false);

		AddTreeButtonIdx++;

		SetTabTitle(AddTreeButtonIdx,"");
		SetTabButtonIcon(AddTreeButtonIdx,AddIcon);
		SetTabDisabled(AddTreeButtonIdx,true);
	}

	public void RemoveTab(int idx) {
		var control = GetTabControl(idx);
		RemoveChild(control);

		AddTreeButtonIdx--;

		SetTabTitle(AddTreeButtonIdx,"");
		SetTabButtonIcon(AddTreeButtonIdx,AddIcon);
		SetTabDisabled(AddTreeButtonIdx,true);
	}

	public void _onAddTree(long idx) {
		if(idx == AddTreeButtonIdx) {
			AddTab();
			if(AddTreeButtonIdx - 1 == 0) {
				_onTabSelected(0);
			}
		} else {
			RemoveTab((int)idx);
		}
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
}
